package doc2db.interfaces;

import doc2db.Keey;
import doc2db.UserProp;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
 *
 */
public class TreeTabViewer extends JFrame{

    private JEditorPane pane = new JEditorPane("text/html", "");
    private StringBuffer buffer = new StringBuffer(
            "<html><head><style><!--" +
            "body { font-size: 12;font-family: monospaced;}" +
            "p {MARGIN-LEFT: 20pt;}" +
            "table {size:800}" +
            "--></style></head><body><table>\n");

    public TreeTabViewer(){
        super("doc2db");
        this.initVar();
    }

    private void initVar() {
        setSize(500, 600);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setLocationRelativeTo(null);
        try {
            setIconImage(ImageIO.read(new File(Keey.imgPath + "main.jpg")));
        } catch (IOException ex) {}
        pane.setContentType("text/html");
        add(new JScrollPane(pane), BorderLayout.CENTER);
        pane.setEditable(false);
        pane.setBackground(Color.WHITE);
        pane.setCursor(new Cursor(Cursor.TEXT_CURSOR));
        final JFrame f = this;
        pane.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(e.isPopupTrigger()){
                    JPopupMenu m = new JPopupMenu();
                    JMenuItem clean = new JMenuItem(new AbstractAction("Очистить") {
                        public void actionPerformed(ActionEvent e) {
                            pane.setText("");
                        }
                    });
                    clean.setFont(UserProp.menuFont);
                    m.add(clean);
                    JMenuItem close = new JMenuItem(new AbstractAction("Закрыть") {
                        public void actionPerformed(ActionEvent e) {
                            setVisible(false);
                        }
                    });
                    close.setFont(UserProp.menuFont);
                    m.add(close);
                    JMenuItem bgText = new JMenuItem(new AbstractAction("Исходный текст") {
                        public void actionPerformed(ActionEvent e) {
                            JFrame fr = new JFrame("исходный текст ");
                            fr.setSize(500, 600);
                            fr.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                            fr.setLocationRelativeTo(f);
                            fr.setIconImage(f.getIconImage());
                            JTextArea t = new JTextArea();
                            t.setText(pane.getText());
                            t.setFont(UserProp.textFont);
                            t.setEditable(false);
                            fr.add(new JScrollPane(t));
                            fr.setVisible(true);
                        }
                    });
                    bgText.setFont(UserProp.menuFont);
                    m.add(bgText);
                    m.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });
        
    }

    public void addText(String text){
        buffer.append(text
                .replaceAll("&", "&amp;")
                .replaceAll("LINE_END", "<br>")
                //.replaceAll("<", "&lt;")  .replaceAll(">", "&gt;")
                .replaceAll(" ", "&nbsp;")
                //.replaceAll("\n", "<br>")
                + "\n");
    }
    
    public void reinitBuffer(){
        buffer = new StringBuffer();
        buffer.append("<html><body>\n");
    }

    public void showMe(){
        buffer.append("\n</body></html>\n");
        pane.setText(buffer.toString());
        pane.setSelectionStart(0);
        pane.setSelectionEnd(1);
        setVisible(true);
    }
}
